package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.MatchDAO;
import com.chanyachting.emall.domain.Match;
import com.chanyachting.emall.service.MatchService;

public class MatchServiceImpl implements MatchService {

	private MatchDAO cDao;

	public MatchServiceImpl() {

		this.cDao = new MatchDAO();
	}
	
	@SuppressWarnings("unchecked")
	public List<Match> getMatchListByPage(int currentPage,int pageSize,String channelId) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("from Match where recommend = '1'");
		if (channelId != null && !"".equals(channelId )) {
			sb.append(" and channelId = " + channelId);
		}
		sb.append(" order by updateDate desc");
		Query q = this.cDao.createQuery(sb.toString());
		
		q.setFirstResult((currentPage - 1) * pageSize);
		q.setMaxResults(pageSize);
		
		return q.list();
	}

	public int getMatchCount(String channelId) {
		String sql = "select count(*) from Match where recommend = '1'";
		if (channelId != null && !"".equals(channelId )) {
			sql = sql + " and channelId = " + channelId;
		}
		
		return (Integer)cDao.find(sql).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Match getOneMatchById(String id) {
		String sql = "from Match where id = ? and status = '01'";
		
		List<Match> matchList = cDao.find(sql, new String[]{id});
		
		if (matchList == null || matchList.size() != 1) {
			return null;
		} else {
			return matchList.get(0);
		}
	}
}
